﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Collect_Request : System.Web.UI.Page
    {
        IBLLPrePurchaseRequest bllPPR = new PrePurchaseRequestBLL();
        IBLLPrePurchaseRequestItem bllPprItem = new PrePurchaseRequestItemBLL();
        IBLLSysYearType bllYearType = new SysYearTypeBLL();
        SysControlBLL bllSysCon = new SysControlBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDefault();
                LoadGridAll();
                LoadItemName();
                LoadYearType();
            }
        }
        protected void LoadDefault()
        {
            SysControlInfo infoDefault = bllSysCon.GetDefaultData();
            if (infoDefault.YearType != null)
            {
                cbYearType.SelectedValue = infoDefault.YearType.ToString();
                Session["yearSeq"] = cbYearType.SelectedValue;
                Session["yearSeq_Urgent"] = cbYearType.SelectedValue;

            }
            if (infoDefault.Year != null)
            {
                txtYear.Text = infoDefault.Year.ToString();
                Session["year"] = txtYear.Text;
                Session["year_Urgent"] = txtYear.Text;
            }
        }
        private void LoadGridAll()
        {
            IList<PrePurchaseRequestInfo> InfoPpr = bllPPR.GetCollectRequest();
            Grid_All.DataSource = InfoPpr;
            //GridGroupByExpression g;
            //Grid_All.MasterTableView.GroupByExpressions.Clear();
            //g = new GridGroupByExpression(Grid_All.Columns[0]);
            //Grid_All.MasterTableView.GroupByExpressions.Add(g);
            //g = new GridGroupByExpression(Grid_All.Columns[1]);
            //Grid_All.MasterTableView.GroupByExpressions.Add(g);
            //g = new GridGroupByExpression(Grid_All.Columns[2]);
            //Grid_All.MasterTableView.GroupByExpressions.Add(g);

            Grid_All.DataBind();
        }

        private void LoadGridSelect()
        {
            IList<PrePurchaseRequestInfo> InfoPpr = new List<PrePurchaseRequestInfo>();
            Nullable<int> ItemSeq = null;

            if (cbItemName.SelectedIndex >= 0)
            {
                ItemSeq = Convert.ToInt32(cbItemName.SelectedValue.ToString());
            }

            InfoPpr = bllPPR.GetCollectRequestGroupItem(ItemSeq);
            Grid_Select.DataSource = InfoPpr;
            //GridGroupByExpression g;
            //Grid_Select.MasterTableView.GroupByExpressions.Clear();
            //g = new GridGroupByExpression(Grid_Select.Columns[1]);
            //Grid_Select.MasterTableView.GroupByExpressions.Add(g);
            //g = new GridGroupByExpression(Grid_Select.Columns[2]);
            //Grid_Select.MasterTableView.GroupByExpressions.Add(g);
            //g = new GridGroupByExpression(Grid_Select.Columns[3]);
            //Grid_Select.MasterTableView.GroupByExpressions.Add(g);
            //g = new GridGroupByExpression(Grid_Select.Columns[4]);
            //Grid_Select.MasterTableView.GroupByExpressions.Add(g);
            //g = new GridGroupByExpression(Grid_Select.Columns[5]);
            //Grid_Select.MasterTableView.GroupByExpressions.Add(g);

            Grid_Select.DataBind();
        }

        protected void LoadYearType()
        {
            IList<SysYearTypeInfo> infoList = bllYearType.GetSysYearType();
            cbYearType.DataSource = infoList;
            cbYearType.DataBind();
        }

        private void LoadItemName()
        {
            IList<PrePurchaseRequestItemInfo> InfoPprItem = bllPprItem.GetCollectItemName();
            cbItemName.DataSource = InfoPprItem;
            cbItemName.DataBind();
        }

        protected void Grid_AllCommand(object source, GridCommandEventArgs e)
        {
            LoadGridAll();
        }

        protected void Grid_SelectCommand(object source, GridCommandEventArgs e)
        {
            LoadGridSelect();
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            if (RadTabStrip1.SelectedTab.Value == "All")
            {
                LoadGridAll();
            }
            else
            {
                LoadGridSelect();
            }
        }

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            LoadGridAll();
            btnCancel.Visible = true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            LoadGridAll();
            btnCancel.Visible = false;
        }

        protected void btnDisplay_Collect_Click(object sender, EventArgs e)
        {
            LoadGridSelect();
            btnCancel_Collect.Visible = true;
        }

        protected void btnCancel_Collect_Click(object sender, EventArgs e)
        {
            LoadGridSelect();
            btnCancel_Collect.Visible = false;
        }

        protected void RadManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            if (e.Argument == "CloseWindow")
            {
                LoadGridAll();
            }
            else if (e.Argument == "CloseWindowCollect")
            {
                LoadGridSelect();
            }
        }

        protected void btnCollect_Click(object sender, EventArgs e)
        {
            IList<PrePurchaseRequestInfo> InfoPprSelected = new List<PrePurchaseRequestInfo>();
            string YearType = "";
            string Year = "";
            string PcRule = "";
            string PcType = "";
            IList<int> PprSeqList = new List<int>();
            foreach (GridDataItem item in Grid_Select.SelectedItems)
            {
                if (YearType == "" && Year == "")
                {
                    YearType = item["InfoYearType.Seq"].Text;
                    Year = item["Year"].Text;
                    PcRule = item["InfoPurchaseRule.Seq"].Text;
                    PcType = item["InfoPurchaseType.Seq"].Text;
                }

                if (YearType != item["InfoYearType.Seq"].Text && Year != item["Year"].Text)
                {
                    //มีรายการ ประเภทปี ไม่ตรงกัน
                    RadManager1.Alert("ไม่สามารถรวบรวมรายการที่ประเภทปีและปีแตกต่างกันได้");
                    return;
                }

                if (PcRule != item["InfoPurchaseRule.Seq"].Text)
                {
                    //มีรายการ ระเบียบจัดหา ไม่ตรงกัน
                    RadManager1.Alert("ไม่สามารถรวบรวมรายการที่ระเบียบการจัดหาแตกต่างกันได้");
                    return;
                }

                if (PcType != item["InfoPurchaseType.Seq"].Text)
                {
                    //มีรายการ ประเภทการจัดหา ไม่ตรงกัน
                    RadManager1.Alert("ไม่สามารถรวบรวมรายการที่ประเภทการจัดหาแตกต่างกันได้");
                    return;
                }

                PrePurchaseRequestInfo info = new PrePurchaseRequestInfo();
                PrePurchaseRequestItemInfo infoItem = new PrePurchaseRequestItemInfo();
                MasterPurchaseTypeInfo infoPcType = new MasterPurchaseTypeInfo();
                MasterPurchaseRuleInfo infoPcRule = new MasterPurchaseRuleInfo();
                SysYearTypeInfo infoYearType = new SysYearTypeInfo();
                SysPprTypeInfo infoPprType = new SysPprTypeInfo();
                info.Seq = Convert.ToInt32(item["Seq"].Text);
                info.YearTypeDescr = item["YearTypeDescr"].Text;
                infoItem.Seq = Convert.ToInt32(item["InfoPprItem.Seq"].Text);
                infoPcType.Seq = Convert.ToInt32(item["InfoPurchaseType.Seq"].Text);
                infoPcType.Name = item["InfoPurchaseType.Name"].Text;
                infoPcRule.Seq = Convert.ToInt32(item["InfoPurchaseRule.Seq"].Text);
                infoPcRule.Name = item["InfoPurchaseRule.Name"].Text;
                infoPprType.Seq = Convert.ToInt32(item["InfoPprType.Seq"].Text);
                infoYearType.Seq = Convert.ToInt32(item["InfoYearType.Seq"].Text);
                info.Year = Convert.ToInt32(item["Year"].Text);
                info.InfoPprItem = infoItem;
                info.InfoPurchaseType = infoPcType;
                info.InfoPurchaseRule = infoPcRule;
                info.InfoPprType = infoPprType;
                info.InfoYearType = infoYearType;
                InfoPprSelected.Add(info);
            }

            if (InfoPprSelected.Count > 0)
            {
                Session["InfoPprSelected"] = InfoPprSelected;
                RadManager1.ResponseScripts.Add(@"openWinCollect();");
            }
            else
            {
                RadManager1.Alert("กรุณาเลือกรายการที่ต้องการรวบรวม");
                return;
            }
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/Personal_Data.ascx"));
        }
    }
}
